// DONE:Initialze new and edit object from dialog
$(document).ready(function() {

        //log('editObject.js: document.ready')

        newObjectDialog = function(url)
        {  
            myObject[0]['ID'] = null;  
            myObject[0]['mode'] = 'NEW';  
            var url = url;
            var dialogClass = 'editDialog';
            var dialogTitle = 'New Object';
            var formToSubmit = '#editObjectDialog_form';
            var buttons = [
                {
                    id: 'editObjectButton',
                    text: "OK",
                    click: function() { jQuery(formToSubmit).trigger('submit'); }
                }
            ];            
            openNewDialogOnOpen = function() {

                $('#'+buttons[0].id).button("option", "disabled", true);

                // Override generation of error label
                jQuery(formToSubmit).validate({
                        rules: {
                            /*newObjectDialog_object_type: { check_item_dropdown: true },
                            name: 'required' */
                        }, 
                        highlight: function(element) {
                            $(element).parent().parent().addClass("error");
                        },
                        unhighlight: function(element) {
                            $(element).parent().parent().removeClass("error");
                        },
                        submitHandler: function(form) {

                            //disable add button                            
                            $('#'+buttons[0].id).button("option", "disabled", true);

                            $('.'+dialogClass+' .status_info').html(loader_html());

                            var form = $(formToSubmit);
                            var form_data = form.serialize();

                            $.ajax({
                                    url: form.attr('action'),
                                    dataType: 'json',
                                    type: "POST",
                                    data: form_data,
                                    success: function (data, textStatus, xhr) {

                                        //return false;

                                        if(data.action==true){

                                            $('.'+dialogClass+' .status_info').html(alert_html(data.msg,'success')); 

                                            function action_delay(){
                                                $('.'+dialogClass).dialog("close");
                                                location.reload();    
                                            }
                                            window.setTimeout(function() { action_delay(); }, 1001);

                                        }else{

                                            $('#'+buttons[0].id).button("option", "disabled", false);
                                            $('.'+dialogClass+' .status_info').html(alert_html(data.msg,'error'));
                                            $('.'+dialogClass).animate({
                                                    scrollTop: $('.'+dialogClass+' .status_info').offset().top
                                                }, 200);
                                        }
                                    },
                                    error: function (xhr, textStatus, errorThrown) {
                                        //enable add button                            
                                        $('#'+buttons[0].id).button("option", "disabled", false); 
                                        $('.'+dialogClass+' .status_info').html(alert_html('Error ocured...\n'+xhr+'\n'+textStatus+errorThrown+'\n','error'));
                                    }
                            });
                        },
                        errorPlacement: function(error, element){}
                })

                //log(myObject[0])


            }  
            var DialogOnOpen = openNewDialogOnOpen;

            newDialog(url,dialogClass,dialogTitle,buttons,DialogOnOpen,1210,$(window).height()-40);
        }

        editObjectDialog = function(url)
        {   
            myObject[0]['mode'] = 'EDIT';
            var url = url;
            var dialogClass = 'editDialog';
            var dialogTitle = 'Edit Object';
            var formToSubmit = '#editObjectDialog_form';
            var buttons = [
                {
                    id: 'editObjectButton',
                    text: "OK",
                    click: function() { jQuery(formToSubmit).trigger('submit'); }
                }
            ];            
            openEditDialogOnOpen = function() {

                $('#'+buttons[0].id).button("option", "disabled", true);

                // Override generation of error label
                jQuery(formToSubmit).validate({
                        rules: {
                            /*newObjectDialog_object_type: { check_item_dropdown: true },
                            name: 'required' */
                        }, 
                        highlight: function(element) {
                            $(element).parent().parent().addClass("error");
                        },
                        unhighlight: function(element) {
                            $(element).parent().parent().removeClass("error");
                        },
                        submitHandler: function(form) {

                            //disable add button                            
                            $('#'+buttons[0].id).button("option", "disabled", true);

                            $('.'+dialogClass+' .status_info').html(loader_html());

                            var form = $(formToSubmit);
                            var form_data = form.serialize();

                            $.ajax({
                                    url: form.attr('action'),
                                    dataType: 'json',
                                    type: "POST",
                                    data: form_data,
                                    success: function (data, textStatus, xhr) {

                                        //return false;

                                        if(data.action==true){

                                            $('.'+dialogClass+' .status_info').html(alert_html(data.msg,'success')); 

                                            function action_delay(){                                                
                                                $('.'+dialogClass).dialog("close");
                                                location.reload();   
                                            }
                                            window.setTimeout(function() { action_delay(); }, 1001);

                                        }else{

                                            $('#'+buttons[0].id).button("option", "disabled", false);
                                            $('.'+dialogClass+' .status_info').html(alert_html(data.msg,'error'));
                                            $('.'+dialogClass).animate({
                                                    scrollTop: $('.'+dialogClass+' .status_info').offset().top
                                                }, 200);
                                        }
                                    },
                                    error: function (xhr, textStatus, errorThrown) {
                                        //enable add button                            
                                        $('#'+buttons[0].id).button("option", "disabled", false); 
                                        $('.'+dialogClass+' .status_info').html(alert_html('Error ocured...\n'+xhr+'\n'+textStatus+errorThrown+'\n','error'));
                                    }
                            });
                        },
                        errorPlacement: function(error, element){}
                })

                //test code Edit Attribute
                //openEditAttributes();

                //test code Add Object
                //submitAddNewObject();

                // select dropdowns
                $("#editObjectDialog_object_type").val(editObjectDialog_object_type);
                $("#editObjectDialog_object_type").attr("disabled","disabled");

                //disable type and set attributes visible
                $("#editObjectDialog_owner").val(editObjectDialog_owner);
                $("."+editObjectDialog_object_type).removeClass('hidden');

                //add input hidden object type for submission
                $('<input>').attr({
                        type: 'hidden',
                        id: 'editObjectDialog_object_type',
                        name: 'editObjectDialog_object_type',
                        value: editObjectDialog_object_type
                }).appendTo(formToSubmit);

                // for object_type change event
                myObject[0]['ID'] = editObjectDialog_post_ID;

                //log(myObject[0])

                //get fields
                get_object_fields(editObjectDialog_object_type,base_url+'objects/form_edit',myObject[0]['ID']);

                $('#'+buttons[0].id).button("option", "disabled", false);


            }  
            var DialogOnOpen = openEditDialogOnOpen;

            newDialog(url,dialogClass,dialogTitle,buttons,DialogOnOpen,1210,$(window).height()-40);
        }

        // Must be in editObject.js due to adding multiple change event on load
        $('#editObjectDialog_object_type').live('change',function(){

                //DONE: rename $("#addNewObject").button("option", "disabled", false);
                myObject[0]['type'] = $(this).val();
                switch(myObject[0]['type']){
                    case 'apartment':
                        $('.apartment').removeClass('hidden');
                        $('.hotel').addClass('hidden');
                        $('.villa').addClass('hidden');
                        $("#editObjectButton").button("option", "disabled", false);
                        myObject[0]['type'] = 'apartment';
                        break;
                    case 'hotel':
                        $('.apartment').addClass('hidden');
                        $('.hotel').removeClass('hidden');
                        $('.villa').addClass('hidden');
                        $("#editObjectButton").button("option", "disabled", false);
                        myObject[0]['type'] = 'hotel';
                        break;
                    case 'villa':
                        $('.apartment').addClass('hidden');
                        $('.hotel').addClass('hidden');
                        $('.villa').removeClass('hidden');
                        $("#editObjectButton").button("option", "disabled", false);
                        myObject[0]['type'] = 'villa';
                        break;
                    default:
                        $('.apartment').addClass('hidden');
                        $('.hotel').addClass('hidden');
                        $('.villa').addClass('hidden');
                        $("#editObjectButton").button("option", "disabled", true);
                        myObject[0]['type'] = null;
                        return false;
                }

                get_object_fields(
                    myObject[0]['type'],
                    myObject[0]['formViewUrl'],
                    myObject[0]['ID']
                );

                //log('change');

                return false;

        });          


});